<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="昵称" prop="nickName">
                <el-input v-model="queryParams.nickName" placeholder="请输入昵称"
                          clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="手机号" prop="customerPhone">
                <el-input v-model="queryParams.customerPhone" placeholder="请输入手机号"
                          clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="归属代理" prop="agentPhone">
                <el-input v-model="queryParams.agentPhone" placeholder="请输入归属代理手机号"
                          clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="注册时间">
                <el-date-picker v-model="dateRange" size="small" style="width: 360px" value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetimerange" range-separator="-" :default-time="['00:00:00', '23:59:59']" start-placeholder="开始日期" end-placeholder="结束日期"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                    v-model="queryParams.status"
                    placeholder="请输入状态"
                    clearable
                    size="small"
                    style="width: 240px"
                >
                    <el-option
                        v-for="dict in sysStatus"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
<!--            <el-col :span="1.5">-->
<!--                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"-->
<!--                           v-hasPermi="['bs:customer:saveOrUpdate']"-->
<!--                >新增</el-button>-->
<!--            </el-col>-->
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                        v-hasPermi="['bs:customer:export']"
                >导出</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
            <el-table-column label="ID" align="center" prop="id" :show-overflow-tooltip="true" />
            <el-table-column label="昵称" align="center" prop="nickName" :show-overflow-tooltip="true" />
            <el-table-column label="头像" align="center" prop="imgUrl" :show-overflow-tooltip="true" >
                <template slot-scope="scope">
                    <el-image
                        style="width: 40px; height: 40px"
                        :src="scope.row.imgUrl"
                        :preview-src-list="[scope.row.imgUrl]"
                    >
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column label="手机号" align="center" prop="customerPhone" :show-overflow-tooltip="true" />
            <el-table-column label="注册时间" align="center" prop="registerTime" width="180"/>
            <el-table-column label="所属渠道" align="center" prop="belongTag" width="100" :formatter="statusFormat"/>
            <el-table-column label="签约信息" align="center" :show-overflow-tooltip="true" v-if="this.$store.getters.loginUser.roles[0].roleId !== 2">
                <template slot-scope="scope">
                    <span class="link-type" @click="handleShow(scope.row)">
                        查看
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="产品数" align="center" prop="proNum" :show-overflow-tooltip="true" />
            <el-table-column label="归属代理" align="center" prop="agentPhone" :show-overflow-tooltip="true" v-if="this.$store.getters.loginUser.roles[0].roleId !== 2" />
            <el-table-column label="代理比例" align="center" prop="agentRateStr" :show-overflow-tooltip="true" >
                <template slot-scope="scope">
                    {{ scope.row.agentRateStr || "-" }}
                </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status" width="100">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        active-value="0"
                        inactive-value="1"
                        @change="handleStatusChange(scope.row)"
                    ></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="this.$store.getters.loginUser.roles[0].roleId !== 2">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                            v-hasPermi="['bs:customer:saveOrUpdate']">修改</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.nowPage" :limit.sync="queryParams.pageSize" @pagination="getPage"/>
        <!-- 查看签约信息对话框 -->
        <el-dialog :title="title" :visible.sync="showOpen" width="800px"  v-dialog-drag :close-on-click-modal='false'>
            <el-form ref="showForm" :model="showForm" :rules="rules" label-width="120px" >
                <el-form-item label="签约类型" prop="contractType">
                    <el-radio-group v-model="showForm.contractType" disabled="true">
                        <el-radio
                            v-for="dict in contract_type"
                            :key="dict.dictValue"
                            :label="dict.dictValue"
                        >{{dict.dictLabel}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="签约姓名" prop="contractName">
                    <el-input v-model="showForm.contractName" style="width: 450px;"  type="text"  disabled  />
                </el-form-item>
                <el-form-item label="身份证号" prop="cardId">
                    <el-input v-model="showForm.cardId" style="width: 450px;"  type="text"  disabled />
                </el-form-item>
                <el-form-item label="联系电话" prop="phone">
                    <el-input v-model="showForm.phone" style="width: 450px;"  type="text"  disabled  />
                </el-form-item>
                <el-form-item label="银行卡号" prop="bankNo">
                    <el-input v-model="showForm.bankNo" style="width: 450px;"  type="text"  disabled placeholder="请输入银行卡号" />
                </el-form-item>
                <el-form-item label="开户银行" prop="bankDesc">
                    <el-input v-model="showForm.bankDesc" style="width: 450px;"  type="text"  disabled placeholder="请输入开户银行" />
                </el-form-item>
                <el-form-item label="所属渠道" prop="belongTag">
                    <el-select
                        v-model="showForm.belongTag"
                        placeholder="所属渠道"
                        clearable
                        style="width: 450px"
                        disabled="true"
                        size="small">
                        <el-option
                            v-for="dict in belong_tag"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                        />
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelShow">关闭</el-button>
            </div>
        </el-dialog>
        <!-- 添加或修改客户管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px"  v-dialog-drag :close-on-click-modal='false'>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="昵称" prop="nickName">
                    <el-input v-model="form.nickName" style="width: 450px;"  type="text" placeholder="请输入昵称"/>
                </el-form-item>
                <el-form-item label="头像" prop="imgUrl">
                    <el-image
                        style="width: 50px; height: 50px"
                        :src="form.imgUrl"
                        v-if="form.imgUrl"
                        :preview-src-list="[form.imgUrl]"
                    >
                    </el-image>
                    <span v-else>无</span>
                </el-form-item>
                <el-form-item label="手机号" prop="customerPhone">
                    <el-input v-model="form.customerPhone" style="width: 450px;"  type="text" placeholder="请输入手机号"/>
                </el-form-item>
                <el-form-item label="注册时间" prop="registerTime">
                    <el-date-picker type="datetime" v-model="form.registerTime" format="yyyy-MM-dd HH:mm:ss"
                                    value-format="yyyy-MM-dd HH:mm:ss" style="width: 450px;" placeholder="请选择注册时间" clearable disabled>
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="归属代理" prop="agentPhone">
                    <el-input v-model="form.agentPhone" style="width: 450px;"  type="text" placeholder="请输入归属代理" />
                </el-form-item>
                <el-form-item label="代理比例（%）" prop="agentRate">
                    <el-input v-model="form.agentRate" style="width: 450px;"  type="text" placeholder="请输入代理比例" />
                </el-form-item>
                <el-form-item label="所属渠道" prop="belongTag">
                    <el-select
                        v-model="form.belongTag"
                        placeholder="所属渠道"
                        clearable
                        style="width: 450px"
                        size="small">
                        <el-option
                            v-for="dict in belong_tag"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in sysStatus"
                            :key="dict.dictValue"
                            :label="dict.dictValue"
                        >{{dict.dictLabel}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import {getBsCustomerPage,getBsCustomerById,addOrUpdateBsCustomer,delBsCustomer,exportBsCustomer} from "@/api/project/bsCustomer/bsCustomer";
import {getBsCustomerContractById} from  "@/api/project/bsCustomer/bsCustomerContract";
export default {
    name: "bsCustomer",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 参数表格数据
            configList: [],
            // 弹出层标题
            title: "",
            open: false,// 是否显示修改或增加的弹出层
            // 日期范围
            dateRange: [],
            // 状态数据字典
            sysStatus: [],
            // 签约类型数据字典
            contract_type: [],
            // 所属渠道
            belong_tag: [],
            // 查询参数
            queryParams: {
                nowPage: 1,
                pageSize: 10,
                agentId: undefined,
                agentPhone: undefined,
                agentRate: undefined,
                createBy: undefined,
                customerPhone: undefined,
                imgUrl: undefined,
                nickName: undefined,
                openId: undefined,
                proNum: undefined,
                status: undefined,
                updateBy: undefined,
            },
            // 表单参数
            form: {},
            showOpen: false,//是否显示查看工作内容弹出层
            showForm: {},
            // 表单校验
            rules: {
                customerPhone: [
                    {required: true, message: "手机号不能为空", trigger: "blur"}
                ],
                nickName: [
                    {required: true, message: "昵称不能为空", trigger: "blur"}
                ],
                status: [
                    {required: true, message: "状态不能为空", trigger: "blur"}
                ]
            },
        };
    },
    created() {
        this.getPage();
        this.getDicts("sys_normal_disable").then(response => {
            this.sysStatus = response.data;
        });
        this.getDicts("contract_type").then(response => {
            this.contract_type = response.data;
        });
        // 所属渠道
        this.getDicts("belong_tag").then(response => {
            this.belong_tag = response.data;
        });
    },
    methods: {
        /** 查询参数列表 */
        getPage(e) {
            this.loading = true;
            let agentId = this.queryParams.agentId || '';
            let agentPhone = this.queryParams.agentPhone || '';
            let agentRate = this.queryParams.agentRate || '';
            let createBy = this.queryParams.createBy || '';
            let customerPhone = this.queryParams.customerPhone || '';
            let imgUrl = this.queryParams.imgUrl || '';
            let nickName = this.queryParams.nickName || '';
            let openId = this.queryParams.openId || '';
            let proNum = this.queryParams.proNum || '';
            let status = this.queryParams.status || '';
            let updateBy = this.queryParams.updateBy || '';
            let time = this.dateRange || '';
            let params = '';
            if (e) {
                params = {
                    agentId: agentId,
                    agentPhone: agentPhone,
                    agentRate: agentRate,
                    createBy: createBy,
                    customerPhone: customerPhone,
                    imgUrl: imgUrl,
                    nickName: nickName,
                    openId: openId,
                    proNum: proNum,
                    status: status,
                    updateBy: updateBy,
                    startTime: time[0],
                    endTime: time[1],
                    nowPage: e.page,
                    pageSize: e.limit
                }
            } else {
                params = {
                    agentId: agentId,
                    agentPhone: agentPhone,
                    agentRate: agentRate,
                    createBy: createBy,
                    customerPhone: customerPhone,
                    imgUrl: imgUrl,
                    nickName: nickName,
                    openId: openId,
                    proNum: proNum,
                    status: status,
                    updateBy: updateBy,
                    startTime: time[0],
                    endTime: time[1],
                    nowPage: this.queryParams.nowPage,
                    pageSize: this.queryParams.pageSize
                }
            }
            getBsCustomerPage(params).then(response => {
                this.configList = response.data.records;
                this.total = response.data.total;
                this.loading = false;
            })
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                agentId: undefined,
                agentPhone: undefined,
                agentRate: undefined,
                createBy: undefined,
                createTime: undefined,
                customerPhone: undefined,
                imgUrl: undefined,
                nickName: undefined,
                openId: undefined,
                proNum: undefined,
                registerTime: undefined,
                status: undefined,
                updateBy: undefined,
                updateTime: undefined,
            };
            this.showForm = {
                agentId: undefined,
                agentPhone: undefined,
                agentRate: undefined,
                createBy: undefined,
                createTime: undefined,
                customerPhone: undefined,
                imgUrl: undefined,
                nickName: undefined,
                openId: undefined,
                proNum: undefined,
                registerTime: undefined,
                status: undefined,
                updateBy: undefined,
                updateTime: undefined,
            };
            this.resetForm("form");
            this.resetForm("showForm");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            let agentId = this.queryParams.agentId || '';
            let agentPhone = this.queryParams.agentPhone || '';
            let agentRate = this.queryParams.agentRate || '';
            let createBy = this.queryParams.createBy || '';
            let customerPhone = this.queryParams.customerPhone || '';
            let imgUrl = this.queryParams.imgUrl || '';
            let nickName = this.queryParams.nickName || '';
            let openId = this.queryParams.openId || '';
            let proNum = this.queryParams.proNum || '';
            let status = this.queryParams.status || '';
            let updateBy = this.queryParams.updateBy || '';
            let time = this.dateRange || '';
            let params = {
                agentId: agentId,
                agentPhone: agentPhone,
                agentRate: agentRate,
                createBy: createBy,
                customerPhone: customerPhone,
                imgUrl: imgUrl,
                nickName: nickName,
                openId: openId,
                proNum: proNum,
                status: status,
                updateBy: updateBy,
                startTime: time[0],
                endTime: time[1],
                nowPage: 1,
                pageSize: this.queryParams.pageSize
            };
            getBsCustomerPage(params).then(res => {
                if (res.code == 200) {
                    this.configList = res.data.records;
                    this.total = res.data.total;
                    this.loading = false;
                }
            })
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.queryParams.agentId = '';
            this.queryParams.agentPhone = '';
            this.queryParams.agentRate = '';
            this.queryParams.createBy = '';
            this.queryParams.customerPhone = '';
            this.queryParams.imgUrl = '';
            this.queryParams.nickName = '';
            this.queryParams.openId = '';
            this.queryParams.proNum = '';
            this.queryParams.status = '';
            this.queryParams.updateBy = '';
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.form.status = '0';
            this.open = true;
            this.title = "添加客户管理信息";
        },
        //多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length
        },
        /** 签约信息 */
        handleShow(row) {
            this.reset();
            const id = row.id || '';
            getBsCustomerContractById({id}).then(response => {
                if(response.data) {
                    this.showForm = response.data;
                }
                this.showOpen = true;
                this.title = "签约信息";
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || '';
            getBsCustomerById({id}).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改客户管理信息";
            });
        },
        cancelShow: function () {
            this.showOpen = false;
        },
        /** 状态修改 */
        handleStatusChange(row) {
            let text = row.status === "0" ? "停用" : "启用";
            this.$confirm('确认要"' + text + '""' + row.nickName + '"吗?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return addOrUpdateBsCustomer(row);
            }).then(() => {
                this.msgSuccess(text + "成功");
            }).catch(function() {
                row.status = row.status === "0" ? "1" : "0";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id !== undefined) {
                        addOrUpdateBsCustomer(this.form).then(res => {
                            if (res.code == 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getPage();
                            } else {
                                this.msgError(res.msg);
                            }
                        })
                    } else {
                        addOrUpdateBsCustomer(this.form).then(res => {
                            if (res.code == 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getPage();
                            } else {
                                this.msgError(res.msg);
                            }
                        })
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除客户管理编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delBsCustomer(ids);
            }).then(() => {
                this.getPage();
                this.msgSuccess("删除成功");
            }).catch(function () {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有客户管理数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return exportBsCustomer(queryParams);
            }).then(res => {
                this.download(res.msg);
            }).catch(function () {
            });
        },
        /**
         * 字典状态转换
         */
        statusFormat(row, column) {
            return this.selectDictLabel(this.belong_tag, row.belongTag);
        },
    }
};
</script>
<style>

</style>
